# \src\models\sys_objects_field_rule.py
from sqlalchemy import Column, String, DateTime, Boolean, Integer, Text, Index
from src.core.ormdb import Base
from src.core import str_utils as strU


class SysObjectsFieldRule(Base):
    __tablename__ = "sys_objects_field_rule"

    # 主键
    id = Column(String(36), primary_key=True, default=lambda: strU.generate_id())

    # 业务关联
    api_id = Column(String(36), index=True)
    field_id = Column(String(36), nullable=False, comment="对应 sys_objects_field.id")
    rule_type = Column(String(30), nullable=False, comment="validation / placeholder / tooltip / disableWhen")
    rule_key = Column(String(50), nullable=False, comment="规则名：required、regex、min、max、disableWhen 等")
    rule_value = Column(Text, nullable=False, comment="规则值：true、10、^[0-9]+$、form.type==='a' 等")
    priority = Column(Integer, default=0, comment="同字段内执行顺序，越小越先")
    enabled = Column(Boolean, default=True, comment="是否启用本条规则")

    # 软删除
    deletion_mark = Column(Boolean, default=False)
    deletion_reason = Column(String(255))
    deletion_date = Column(DateTime)
    deletion_by = Column(String(255))

    # 审计
    create_date = Column(DateTime, default=lambda: strU.now(), comment="创建日期，记录创建的时间")
    creator_by = Column(String(255), nullable=True, comment="创建者，记录创建操作的执行者")
    last_updated_date = Column(DateTime, nullable=True, comment="最后更新时间，记录最后更新的时间")
    last_updated_by = Column(String(255), nullable=True, comment="最后更新者，记录最后更新操作的执行者")

    # 索引
    __table_args__ = (
        Index("idx_SysObjectsFieldRule_field_id_type", "field_id", "rule_type"),
        Index("idx_SysObjectsFieldRule_api_id", "api_id"),
        {"comment": "字段级规则库（校验/占位/提示/禁用）"},
    )
